/*
 Copyright (c) 2011 LIX, Ecole Polytechnique, Palaiseau, France

 Title: hazmat variator
 Description: variator for the problem of routing hazardous material in a graph
 Copyright: Copyright (c) 2011
 Company: Ecole Polytechnique
 Authors: Nora Touati-Moungla and Dimo Brockhoff, Ecole Polytechnique, France
 Authors (previous Java version): Tamara Ulrich, ETH Zurich, Switzerland
 */

#include <iostream>
#include <fstream>
//#include <string>
//#include <vector>
#include <exception>

#include "FileManager.h"

/** Handles access to the communication files. It contains methods to read and write the state file and
 * to read out the archive file and the selector file (which are both generated by the selector).
 *
 * @author Tamara Ulrich
 * @version 1.0
 */


	/** Writes the state given in the argument to the state file. The given state has to be a single integer number.
	 *
	 * @param newState the state which has to be written to the state file
	 */
	void FileManager::writeState(int newState){
		try {
			ofstream myfile (Variator::population.stateFile.c_str());
			if (myfile.is_open()) {
				myfile << newState;
				myfile.close();
			}
		}
		catch (exception& e) {
			cerr << "Problem with 'state' file" << endl;
			cerr << e.what() << endl;
		}

	}

	/** Reads and returns the state in the state file. The state file must contain a single integer number.
	 *
	 * @return the number of the current state
	 */
	int FileManager::readState(){
		int state = -1;
		string line;

		try {
			ifstream myfile (Variator::population.stateFile.c_str());

			if (myfile.is_open()) {
				while ( myfile.good() ) {
					getline (myfile, line);
					state = atoi(line.c_str());
				}
				myfile.close();
			}
		}
		catch (exception& e) {
			cerr << "Problem reading 'state' file" << endl;
			cerr << e.what() << endl;
		}
		return state;
	}

	/** Reads the values of an archive or selector file and stores them in an integer vector. Writes 0 into the file
	 * to indicate that it has been read (to prevent multiple readouts). The archive and selector files start with
	 * the number of elements (which is also the length of the return array) and then one element per line, ending
	 * with an <code>END</code> tag on the last line. Each element consists of a single integer number which indicates
	 * the index of the individual in the global population.
	 * <p>
	 * The method terminates if the file contains a single 0, as this means that the file has already been read, which
	 * indicates an error.
	 *
	 * @param filename the name of the file to be read (e.g. "./PISA_arc" or "./PISA_sel"
	 * @return an array where the values in the file are stored
	 */
	vector<int> FileManager::readArcSelFile(string filename) {
		vector<int> values;
		string line;
		int numberOfValues;
		try{
			ifstream myfile (filename.c_str());

			if (myfile.is_open()) {
				// read first line
				getline (myfile, line);
				numberOfValues = atoi(line.c_str());

				if (numberOfValues == 0) {
				cerr << "Error: Arc/Sel file has already been read";
				exit(1);
				}

				// init values to fit all ids
				values.assign(numberOfValues, 0);
				// read values from file
				for (int i = 0; i < numberOfValues; i++) {
					getline (myfile, line);
					values.at(i) = atoi(line.c_str());
				}

				// check for END token...
				getline (myfile, line);
				if (!line.find("END")) {
					cerr << "No END tag found" << flush;
				}

				myfile.close();
			}


			// According to the PISA protocol, a 0 has to be written into the file that has been read (to prevent multiple readouts)
			ofstream myfile2 (filename.c_str());
			if (myfile2.is_open()) {
				myfile2 << "0\r\n";
				myfile2.close();

				return values;
			}
		}
		catch (exception& e) {
			cerr << "Problem reading or writing file " << filename << endl;
			cerr << e.what() << endl;
		}

		// return empty vector if something went wrong
		values.empty();
		return values;
	}

